package _03;

/**
 * Good Question.
 */
public class Solution {
    public static void printNext(int i) {
	if (i <= 0) {
	    System.err.println("i should be positive.");
	    return;
	}

	int rOne = i & (-i);
	int rZero = ~i & (i+1);
	int smaller = -1, larger = -1;
	if (rOne > rZero) {
//	    smaller = (i & ~(1 << rOne)) | (1 << rZero);
//	    int tmp = i | (1 << rZero);
//	    int lZero = ~tmp & (tmp + 1);
//	    larger = (i & ~(1 << rOne)) | (1 << lZero);
		smaller = (i & ~rOne) | rZero;
		int tmp = i | rZero;
		int lZero = ~tmp & (tmp + 1);
		larger = (i & ~rOne) | lZero;
	}
	if (rOne < rZero) {
	    larger = (i & ~(1 << rOne)) | (1 << rZero);
	    int tmp = i & ~(1 << rOne);
	    int lOne = tmp & (-tmp);
	    smaller = (i & ~(1 << lOne)) | (1 << rZero);
	}
	System.out.println("smaller is:\t" + Integer.toBinaryString(smaller));
	System.out.println("origin is:\t" + Integer.toBinaryString(i));
	System.out.println("larger is:\t" + Integer.toBinaryString(larger));
    }
    
    public static void main(String[] args) {
		int i = 0x00000010;
		printNext(i);
	}
}